#!/bin/bash

credFile=/etc/ppp/chap-secrets
passwdFile=/etc/passwd
activeUsersLog=/var/log/shellicio.us_activeusers.log
username="$2"
password="$3"

case "$1" in
	add)
		createdDate="$(date +%s)"
		date="$(date)"

		echo "$date - Adding username $username to VPN..."

		if grep -q "$2" "$credFile";
		then
			echo "$date - Username $username already exists, aborting!"
		else
			echo "$username pptpd $password * #:$createdDate" >> $credFile
			echo "$ip:$createdDate:$username" >> $activeUsersLog
		fi

		echo "$date - Adding username $username to System..."

		sudo adduser $username --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --force-badname
		echo "$username:$password" | sudo chpasswd

		echo "$date - Adding username $username to MySQL and Creating Database..."

		echo "CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';" | mysql -u root -p3lch@rr099
		echo "create database db_$username;" | mysql -u root -p3lch@rr099
		echo "GRANT ALL PRIVILEGES ON db_$username.* TO '$username'@'localhost';" | mysql -u root -p3lch@rr099
	;;
	*)
		echo "Must specificy add username password" 
	;;
esac

